package com.wbdgj.ui.home;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.login.LoginActivity;
import com.wbdgj.ui.mine.VerifiedActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;
import com.wbdgj.utils.dialog.ConfirmDialog;

import java.text.DateFormat;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import butterknife.BindView;
import retrofit2.Call;

public class YhqDetailActivity extends BaseActivity{

    private Context context = this;

    @BindView(R.id.money_wf)
    TextView money_wf;
    @BindView(R.id.content_wf)
    TextView content_wf;
    @BindView(R.id.lx_wf)
    TextView lx_wf;
    @BindView(R.id.title_wf)
    TextView title_wf;
    @BindView(R.id.time_wf)
    TextView time_wf;

    @BindView(R.id.ljlq_wf)
    Button ljlq_wf;
    @BindView(R.id.wfLay)
    LinearLayout  wfLay;

    @BindView(R.id.add_time)
    TextView add_time;
    @BindView(R.id.lqb_time)
    TextView lqb_time;
    @BindView(R.id.lqe_time)
    TextView lqe_time;
    @BindView(R.id.syb_time)
    TextView syb_time;
    @BindView(R.id.sye_time)
    TextView sye_time;

    @BindView(R.id.qsl)
    TextView qsl;

    private String id, branch_id;

    @Override
    protected int getLayoutResId() {
        return R.layout.ui_wb_yhq_detail;
    }

    private void getMbUserInfo() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().getMbUserInfo(BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    if ((linkedTreeMap.get("NAME")+"").equals("null")){
                        launch(VerifiedActivity.class);
                    }else{
                        final ConfirmDialog confirmDialog = new ConfirmDialog(context, "确定领取吗？", 1);
                        confirmDialog.show();
                        confirmDialog.setCanceledOnTouchOutside(false);
                        confirmDialog.setClicklistener(new ConfirmDialog.ClickListenerInterface() {
                            @Override
                            public void doConfirm() {
                                confirmDialog.dismiss();
                                appCoupongetDetail(id);
                            }

                            @Override
                            public void doCancel() {
                                confirmDialog.dismiss();
                            }
                        });
                    }

                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }
            }
        });
    }

    @Override
    protected void initView() {
        id = getIntent().getStringExtra("id");
        branch_id = getIntent().getStringExtra("branch_id");
        couponInfo(id);
        ljlq_wf.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getMbUserInfo();
            }
        });
    }

    private void appCoupongetDetail(String id) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().appCoupongetDetail(branch_id, id, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void couponInfo(String id) {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().couponInfo(branch_id, id,  BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.TOKEN));
        call.enqueue(new OnResponseListener<ResultObjectModel>(context) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    LinkedTreeMap linkedTreeMapData = (LinkedTreeMap)userModelBaseResultModel.getData();
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) linkedTreeMapData.get("data");

                    DecimalFormat decimalFormat = new DecimalFormat("###################.###########");
                    money_wf.setText(decimalFormat.format(linkedTreeMap.get("amount")));
                    content_wf.setText("消费满"+decimalFormat.format(linkedTreeMap.get("min_amount")) + "元可用");
                    title_wf.setText(linkedTreeMap.get("branch_name")+"");
                    lx_wf.setText(linkedTreeMap.get("type_name")+"");
                    time_wf.setText("每人限领："+ decimalFormat.format(linkedTreeMap.get("quantity")) + "张");

                    add_time.setText(linkedTreeMap.get("name")+"");
                    lqb_time.setText(linkedTreeMap.get("get_beg")+"");
                    lqe_time.setText(linkedTreeMap.get("get_end")+"");
                    syb_time.setText(linkedTreeMap.get("use_beg")+"");
                    sye_time.setText(linkedTreeMap.get("use_end")+"");

                    qsl.setText(decimalFormat.format(linkedTreeMap.get("cnts"))+ "张");
                }else if (userModelBaseResultModel.getResultCode().equals("1111")){
                    ToastUtils.toastShort("账号没有登录或在其它地方登录");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, "");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "2");
                    PushManager.getInstance().unBindAlias(context, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.LOGIN_PHONE), false, BaseApplication.getInstance().getSpUtil().getString(SpKeyUtils.CID));
                    Intent intent = new Intent(context, LoginActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

}
